package com.ctshk.rpc.localfun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.localfun.dto.CostStrategyStockDTO;
import com.ctshk.rpc.localfun.dto.LocalFunOfflineSelCustomDTO;
import com.ctshk.rpc.localfun.dto.LocalFunReservationListDTO;
import com.ctshk.rpc.localfun.entity.LocalFunProduct;
import com.ctshk.rpc.localfun.entity.LocalFunProductCostStrategySpecStock;
import com.ctshk.rpc.localfun.req.LocalFunProductCostReq;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Description
 * @Author wansi <gaylen15527491065@yeah.net>
 * @Date 2021/3/23
 */
public interface LocalFunProductCostStrategySpecStockMapper extends BaseMapper<LocalFunProductCostStrategySpecStock> {

    @Select("<script>"+"SELECT distinct t0.currency_id currencyId,t0.currency ,t0.exchange_rate exchangeRate,t3.reserve_charge_type reserveChargeType,t3.reserve_charge reserveCharge,t1.cost_id costId,t1.cost_price costPrice,t1.total_stock totalStock,t1.surplus_stock surplusStock,t1.sold_stock soldStock,t1.attr1_id attr1Id,t1.attr1_name attr1Name,t1.attr2_id attr2Id,t1.attr2_name attr2Name ,min(t2.time_begin) timeBegin,max(t2.time_end) timeEnd FROM t_local_fun_product_cost_strategy t0 left join t_local_fun_product_cost_strategy_spec_stock t1 on t0.id=t1.cost_id LEFT JOIN t_local_fun_product_cost_strategy_vaild_date t2 ON t1.cost_id = t2.cost_id  left join t_local_fun_product t3 on t3.id = t1.product_id WHERE t1.product_id = #{req.productId} group by attr1Id,attr2Id" + "</script>")
    List<CostStrategyStockDTO> getProductCostList(@Param("req") LocalFunProductCostReq localFunProductCostReq);


    /**
     * 查詢自營渠道加幅信息
     * @param localFunProductCostReq
     * @return
     */
    @Select("<script>"+"select t3.check_type checkType,t1.strategy_id increaseStrategyId,t2.channel_type channelType,t2.visable_type visableType,t3.limit_num limitNum,t3.is_limit isLimit,t3.adjust_type adjustType,t3.relation_id relationId,t3.amount,t3.percent from t_local_fun_product_sale_strategy_vaild_date t1 " +
            "LEFT JOIN t_local_fun_product_channel_offline t2 on t1.strategy_id=t2.strategy_id  LEFT JOIN t_local_fun_product_channel_offline_self_custom t3 on t2.id=t3.offline_id where " +
            "t1.time_begin &lt;=#{localFunProductCostReq.queryDate} and t1.time_end &gt;=#{localFunProductCostReq.queryDate}\n" +
            "AND t2.channel_type=1 and t1.product_id=#{localFunProductCostReq.productId} order by t3.gmt_create desc" + "</script>")
    List<LocalFunOfflineSelCustomDTO> getProductSaleList(@Param("localFunProductCostReq") LocalFunProductCostReq localFunProductCostReq);

    /**
     * 查詢代理商渠道加幅
     * @param localFunProductCostReq
     * @return
     */
    @Select("<script>"+"select t1.strategy_id increaseStrategyId,t2.visable_type visableType,t2.channel_type channelType,t3.limit_num limitNum,t3.is_limit isLimit,t3.adjust_type adjustType,t3.agent_id relationId,t3.amount,t3.percent from t_local_fun_product_sale_strategy_vaild_date t1 " +
            "LEFT JOIN t_local_fun_product_channel_offline t2 on t1.strategy_id=t2.strategy_id  LEFT JOIN t_local_fun_product_channel_offline_agent_custom t3 on t2.id=t3.offline_id where " +
            "t1.time_begin &lt;=#{localFunProductCostReq.queryDate} and t1.time_end &gt;=#{localFunProductCostReq.queryDate}\n" +
            "AND t2.channel_type=2 and t1.product_id=#{localFunProductCostReq.productId} and agent_id=#{localFunProductCostReq.agentId}" + "</script>")
    List<LocalFunOfflineSelCustomDTO> getProductAgentSaleList(@Param("localFunProductCostReq") LocalFunProductCostReq localFunProductCostReq);
}
